//https://leetcode.cn/problems/fruit-into-baskets/submissions/550789824/

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int hash[100001] = { 0 };
        int ret = 0;
        for (int left = 0, right = 0, kinds = 0; right < fruits.size(); right++)
        {
            //进窗口
            if (hash[fruits[right]] == 0)
                kinds++;
            hash[fruits[right]]++;
            //判断
            if (kinds > 2)
            {
                //出窗口
                if (hash[fruits[left]] == 1)
                    kinds--;
                hash[fruits[left++]]--;
            }
            //更新结果
            ret = max(ret, right - left + 1);
        }

        return ret;

    }
};